package com.bing.services.impl;

import com.bing.dao.PostDao;
import com.bing.entity.PStatus;
import com.bing.entity.Post;
import com.bing.services.BlogService;
import com.bing.utils.UtilFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by kaibb on 15-12-5.
 */

@Transactional
@Service
public class BlogServiceImpl implements BlogService {
    public static final String POSTS_PAGED = "from Post where pStatus=:pStatus order by modifyDate desc";
    public static final String POSTS_HOT = "from Post where pStatus=:pStatus order by browses desc, modifyDate desc";
    public static final String POSTS_NUM="select count(1) from Post where pStatus=:pStatus order by modifyDate desc";



    public static final int FIRST_PAGE = 1;
    @Autowired
    private PostDao postDao;

    @Override
    public Long createPost(Post post) {
        Long id = post.getId();
        if (id == null) {
            post.setDates(new Date());
            id = postDao.save(post);
        } else {
            post.setModifyDate(new Date());
            postDao.merger(post);
        }
        return id;
    }

    @Override
    public void updatePost(Post post) {
        postDao.update(post);
    }

    @Override
    public void deletePost(Long id) {
        postDao.delete(id);
    }

    @Override
    public long getPostNum() {
        Map<String, Object> params = new HashMap<>();
        params.put("pStatus", PStatus.PUBLISH);
        return (long) postDao.findByHQLOne(POSTS_NUM,params);
    }

    @Override
    public List<Post> getPostsPaged(int pageNo, int pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("pStatus", PStatus.PUBLISH);
        return postDao.findByPage(pageNo, pageSize, POSTS_PAGED, params);
    }

    @Override
    public List<Post> getHotPosts(int size) {
        Map<String, Object> params = new HashMap<>();
        params.put("pStatus", PStatus.PUBLISH);
        return postDao.findByPage(FIRST_PAGE, size, POSTS_HOT, params);
    }

    @Override
    public List<Post> getPostsPagedByTag(int pageNo, int pageSize,  List<String> tags) {
        Map<String, Object> params = new HashMap<>();
        params.put("tags", tags);
        params.put("pStatus", PStatus.PUBLISH);
        return postDao.findByPage(pageNo, pageSize, "from Post where pStatus=:pStatus and :tags in elements(tags) order by modifyDate desc", params);
    }

    @Override
    public Post getPost(Long id) {
        Post byId = postDao.findById(id);
        byId.setContent(UtilFactory.getMD(byId.getContent()));
        return byId;
    }

    @Override
    public Post getPostByTitle(String title) {
        Map<String, Object> map = new HashMap<>();
        map.put("title", title);
        return (Post) postDao.findByHQLOne("FROM Post where title=:title", map);
    }

    @Override
    public Post getPre(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return postDao.findFirst("FROM Post where id<:id order by id desc",map);
    }

    @Override
    public Post getNext(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        return postDao.findFirst("FROM Post where id>:id order by id",map);
    }

    @Override
    public Set<String> getTags() {
        return new HashSet<>(postDao.getTags());
    }
}
